package com.hsytech.dijiebao.chart;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.utils.Constants;
import com.hsytech.dijiebao.utils.SharedPreferencesTools;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import io.rong.imkit.RongContext;
import io.rong.imkit.RongIM;
import io.rong.imkit.RongIM.UserInfoProvider;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.UserInfo;

public class Frament02 extends Fragment {
    private View root;
    private Button bar_right_top_btn;
    private ImageButton top_back_btn;
    private TextView bar_title_tv;
    private List<UserInfo> ufl = new ArrayList<UserInfo>();
    private RelativeLayout delete_center;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_chart_main, container, false);

        getHttp();
        initTitle();
        ConversationListFragment fragment = new ConversationListFragment();
        Uri uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
                .appendPath("conversationlist")
                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") // ����˽�ĻỰ�Ǿۺ���ʾ
                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "true")// ����Ⱥ��Ự�ۺ���ʾ
                .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "true")// ����������Ự�Ǿۺ���ʾ
                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")// ����ϵͳ�Ự�Ǿۺ���ʾ
                .build();
        fragment.setUri(uri);
        if (TextUtils.isEmpty(SharedPreferencesTools.getTOKEN(getActivity()))) {

        } else {

            FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.rong_content, fragment);
            transaction.commit();
        }

        // // �����Ự����
        // if (RongIM.getInstance() != null)
        // RongIM.getInstance().startPrivateChat(getActivity(), "26594",
        // "title");
        // // �����Ự�б����
        // if (RongIM.getInstance() != null)
        // RongIM.getInstance().startConversationList(getActivity());
        //
        // // �����ۺϻỰ�б����
        // if (RongIM.getInstance() != null)
        // RongIM.getInstance().startSubConversationList(getActivity(),
        // Conversation.ConversationType.GROUP);
        return root;
    }

    private void initTitle() {
        RongIM.setUserInfoProvider(new UserInfoProvider() {
            UserInfo uf;

            @Override
            public UserInfo getUserInfo(String uid) {
                Log.d("mylog", "����");

                for (int j = 0; j < ufl.size(); j++) {
                    uf = ufl.get(j);
                    // RongIM.getInstance().refreshUserInfoCache(uf);
                    // if (uf.getUserId().equals(uid)) {
                    Log.d("mylog", "info����" + uf.getName());
                    return new UserInfo(uf.getUserId(), uf.getName(), uf.getPortraitUri());
                    // }

                }

                return null;
            }
        }, true);
        delete_center = (RelativeLayout) root.findViewById(R.id.delete_center);
        delete_center.setVisibility(View.GONE);
        bar_title_tv = (TextView) root.findViewById(R.id.bar_title_tv);
        bar_title_tv.setText("聊天");
    }

    private void getHttp() {
        List<Conversation> c = RongIMClient.getInstance().getConversationList();
        StringBuffer stringBuilder = new StringBuffer();
        if (c != null) {
            stringBuilder.append(SharedPreferencesTools.getUid(getActivity()));
            stringBuilder.append(",");
            for (int i = 0; i < c.size(); i++) {
                String a = c.get(i).getTargetId();

                stringBuilder.append(a);
                stringBuilder.append(",");

            }
            stringBuilder = stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
            Log.d("mylog", stringBuilder + "");
        }
        if (stringBuilder != null) {
//			RequestParams data = new RequestParams();
//			// data.put("city_ids",
//			data.put("uids", stringBuilder);
//			data.put("token", SharedPreferencesTools.getTOKEN(getActivity()));
//			HttpUtils.get(Constants.HTTP_CHART, data, new JsonResponseHandler() {
//
//				@Override
//				public void onSuccess(JSONObject data) {
//					JSONArray result = data.optJSONArray("result");
//					UserInfo uf = null;
//					for (int i = 0; i < result.length(); i++) {
//						String id = result.optJSONObject(i).optString("id");
//						String nickname = result.optJSONObject(i).optString("nickname");
//						String head_photo = result.optJSONObject(i).optString("head_photo");
//
//						uf = new UserInfo(id, nickname,
//								Uri.parse("http://djb.dev.hsydev.com/Upload/image_sm/" + head_photo));
//						RongIM.getInstance().refreshUserInfoCache(uf);
//						ufl.add(uf);
//
//					}
//					// RongIM.getInstance().refreshUserInfoCache(uf);
//					Log.d("mylog", "httphttp" + ufl.size());
//				}
//			});

        }

    }

    private void refreshUserInfo(UserInfo userInfo) {
        if (userInfo == null || RongIMClient.getInstance() == null) {
            throw new IllegalArgumentException();
        }

        if (RongContext.getInstance() != null) {
            RongContext.getInstance().getUserInfoCache().put(userInfo.getUserId(), userInfo);
        }
    }

    public static void main(String[] args) {

    }

}
